using System;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Application.Dtos;
using Matrix.Notification.Domain.Shared.Notifications;

namespace Matrix.Notification.Notifications
{
    /// <summary>
    /// 通知DTO
    /// </summary>
    public class NotificationDto : FullAuditedEntityDto<Guid>
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid? TenantId { get; set; }

        /// <summary>
        /// 通知标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 通知内容
        /// </summary>
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型
        /// </summary>
        public NotificationType Type { get; set; }

        /// <summary>
        /// 通知优先级
        /// </summary>
        public NotificationPriority Priority { get; set; }

        /// <summary>
        /// 通知状态
        /// </summary>
        public NotificationStatus Status { get; set; }

        /// <summary>
        /// 接收者ID
        /// </summary>
        public Guid? RecipientId { get; set; }

        /// <summary>
        /// 接收者用户名
        /// </summary>
        public string? RecipientUserName { get; set; }

        /// <summary>
        /// 接收者邮箱
        /// </summary>
        public string? RecipientEmail { get; set; }

        /// <summary>
        /// 接收者手机号
        /// </summary>
        public string? RecipientPhoneNumber { get; set; }

        /// <summary>
        /// 发送者ID
        /// </summary>
        public Guid? SenderId { get; set; }

        /// <summary>
        /// 发送者姓名
        /// </summary>
        public string? SenderName { get; set; }

        /// <summary>
        /// 通知渠道
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 模板ID
        /// </summary>
        public Guid? TemplateId { get; set; }

        /// <summary>
        /// 模板名称
        /// </summary>
        public string? TemplateName { get; set; }

        /// <summary>
        /// 通知参数
        /// </summary>
        public string? Parameters { get; set; }

        /// <summary>
        /// 附件列表
        /// </summary>
        public string? Attachments { get; set; }

        /// <summary>
        /// 调度时间
        /// </summary>
        public DateTime? ScheduledTime { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; }

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetryCount { get; set; }

        /// <summary>
        /// 下次重试时间
        /// </summary>
        public DateTime? NextRetryTime { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime? SentTime { get; set; }

        /// <summary>
        /// 送达时间
        /// </summary>
        public DateTime? DeliveredTime { get; set; }

        /// <summary>
        /// 阅读时间
        /// </summary>
        public DateTime? ReadTime { get; set; }

        /// <summary>
        /// 失败原因
        /// </summary>
        public string? FailureReason { get; set; }

        /// <summary>
        /// 外部ID
        /// </summary>
        public string? ExternalId { get; set; }

        /// <summary>
        /// 批次ID
        /// </summary>
        public Guid? BatchId { get; set; }

        /// <summary>
        /// 分类标签
        /// </summary>
        public string? Category { get; set; }

        /// <summary>
        /// 业务数据
        /// </summary>
        public string? BusinessData { get; set; }

        /// <summary>
        /// 是否静默发送
        /// </summary>
        public bool IsSilent { get; set; }

        /// <summary>
        /// 是否需要确认
        /// </summary>
        public bool RequiresConfirmation { get; set; }

        /// <summary>
        /// 确认截止时间
        /// </summary>
        public DateTime? ConfirmationDeadline { get; set; }

        /// <summary>
        /// 是否已确认
        /// </summary>
        public bool IsConfirmed { get; set; }

        /// <summary>
        /// 确认时间
        /// </summary>
        public DateTime? ConfirmedTime { get; set; }

        /// <summary>
        /// 地理位置限制
        /// </summary>
        public string? LocationRestriction { get; set; }

        /// <summary>
        /// 时间限制
        /// </summary>
        public string? TimeRestriction { get; set; }

        /// <summary>
        /// 设备限制
        /// </summary>
        public string? DeviceRestriction { get; set; }

        /// <summary>
        /// 通知编码
        /// </summary>
        public string NotificationCode { get; set; } = string.Empty;

        /// <summary>
        /// 关联ID
        /// </summary>
        public Guid? RelatedEntityId { get; set; }

        /// <summary>
        /// 关联类型
        /// </summary>
        public string? RelatedEntityType { get; set; }

        /// <summary>
        /// 是否已归档
        /// </summary>
        public bool IsArchived { get; set; }

        /// <summary>
        /// 归档时间
        /// </summary>
        public DateTime? ArchivedTime { get; set; }
    }

    /// <summary>
    /// 创建通知请求DTO
    /// </summary>
    public class CreateNotificationDto
    {
        /// <summary>
        /// 通知标题
        /// </summary>
        [Required]
        [StringLength(500)]
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 通知内容
        /// </summary>
        [Required]
        [StringLength(4000)]
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型
        /// </summary>
        [Required]
        public NotificationType Type { get; set; }

        /// <summary>
        /// 通知优先级
        /// </summary>
        [Required]
        public NotificationPriority Priority { get; set; } = NotificationPriority.Normal;

        /// <summary>
        /// 通知渠道
        /// </summary>
        [Required]
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 接收者ID
        /// </summary>
        public Guid? RecipientId { get; set; }

        /// <summary>
        /// 接收者用户名
        /// </summary>
        [StringLength(100)]
        public string? RecipientUserName { get; set; }

        /// <summary>
        /// 接收者邮箱
        /// </summary>
        [EmailAddress]
        [StringLength(255)]
        public string? RecipientEmail { get; set; }

        /// <summary>
        /// 接收者手机号
        /// </summary>
        [Phone]
        [StringLength(20)]
        public string? RecipientPhoneNumber { get; set; }

        /// <summary>
        /// 发送者ID
        /// </summary>
        public Guid? SenderId { get; set; }

        /// <summary>
        /// 发送者姓名
        /// </summary>
        [StringLength(100)]
        public string? SenderName { get; set; }

        /// <summary>
        /// 模板ID
        /// </summary>
        public Guid? TemplateId { get; set; }

        /// <summary>
        /// 通知参数
        /// </summary>
        public string? Parameters { get; set; }

        /// <summary>
        /// 附件列表
        /// </summary>
        public string? Attachments { get; set; }

        /// <summary>
        /// 调度时间
        /// </summary>
        public DateTime? ScheduledTime { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 最大重试次数
        /// </summary>
        [Range(0, 10)]
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 分类标签
        /// </summary>
        [StringLength(100)]
        public string? Category { get; set; }

        /// <summary>
        /// 业务数据
        /// </summary>
        public string? BusinessData { get; set; }

        /// <summary>
        /// 是否静默发送
        /// </summary>
        public bool IsSilent { get; set; } = false;

        /// <summary>
        /// 是否需要确认
        /// </summary>
        public bool RequiresConfirmation { get; set; } = false;

        /// <summary>
        /// 确认截止时间
        /// </summary>
        public DateTime? ConfirmationDeadline { get; set; }

        /// <summary>
        /// 地理位置限制
        /// </summary>
        public string? LocationRestriction { get; set; }

        /// <summary>
        /// 时间限制
        /// </summary>
        public string? TimeRestriction { get; set; }

        /// <summary>
        /// 设备限制
        /// </summary>
        public string? DeviceRestriction { get; set; }

        /// <summary>
        /// 关联ID
        /// </summary>
        public Guid? RelatedEntityId { get; set; }

        /// <summary>
        /// 关联类型
        /// </summary>
        [StringLength(100)]
        public string? RelatedEntityType { get; set; }
    }

    /// <summary>
    /// 更新通知请求DTO
    /// </summary>
    public class UpdateNotificationDto
    {
        /// <summary>
        /// 通知标题
        /// </summary>
        [StringLength(500)]
        public string? Title { get; set; }

        /// <summary>
        /// 通知内容
        /// </summary>
        [StringLength(4000)]
        public string? Content { get; set; }

        /// <summary>
        /// 通知优先级
        /// </summary>
        public NotificationPriority? Priority { get; set; }

        /// <summary>
        /// 接收者ID
        /// </summary>
        public Guid? RecipientId { get; set; }

        /// <summary>
        /// 接收者用户名
        /// </summary>
        [StringLength(100)]
        public string? RecipientUserName { get; set; }

        /// <summary>
        /// 接收者邮箱
        /// </summary>
        [EmailAddress]
        [StringLength(255)]
        public string? RecipientEmail { get; set; }

        /// <summary>
        /// 接收者手机号
        /// </summary>
        [Phone]
        [StringLength(20)]
        public string? RecipientPhoneNumber { get; set; }

        /// <summary>
        /// 调度时间
        /// </summary>
        public DateTime? ScheduledTime { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 最大重试次数
        /// </summary>
        [Range(0, 10)]
        public int? MaxRetryCount { get; set; }

        /// <summary>
        /// 分类标签
        /// </summary>
        [StringLength(100)]
        public string? Category { get; set; }

        /// <summary>
        /// 业务数据
        /// </summary>
        public string? BusinessData { get; set; }

        /// <summary>
        /// 是否静默发送
        /// </summary>
        public bool? IsSilent { get; set; }

        /// <summary>
        /// 是否需要确认
        /// </summary>
        public bool? RequiresConfirmation { get; set; }

        /// <summary>
        /// 确认截止时间
        /// </summary>
        public DateTime? ConfirmationDeadline { get; set; }

        /// <summary>
        /// 地理位置限制
        /// </summary>
        public string? LocationRestriction { get; set; }

        /// <summary>
        /// 时间限制
        /// </summary>
        public string? TimeRestriction { get; set; }

        /// <summary>
        /// 设备限制
        /// </summary>
        public string? DeviceRestriction { get; set; }
    }

    /// <summary>
    /// 发送通知请求DTO
    /// </summary>
    public class SendNotificationDto
    {
        /// <summary>
        /// 通知标题
        /// </summary>
        [Required]
        [StringLength(500)]
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 通知内容
        /// </summary>
        [Required]
        [StringLength(4000)]
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 通知类型
        /// </summary>
        [Required]
        public NotificationType Type { get; set; }

        /// <summary>
        /// 通知优先级
        /// </summary>
        [Required]
        public NotificationPriority Priority { get; set; } = NotificationPriority.Normal;

        /// <summary>
        /// 通知渠道
        /// </summary>
        [Required]
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 接收者ID
        /// </summary>
        [Required]
        public Guid Recipient { get; set; }

        /// <summary>
        /// 接收者用户名
        /// </summary>
        [StringLength(100)]
        public string? RecipientUserName { get; set; }

        /// <summary>
        /// 接收者邮箱
        /// </summary>
        [EmailAddress]
        [StringLength(255)]
        public string? RecipientEmail { get; set; }

        /// <summary>
        /// 接收者手机号
        /// </summary>
        [Phone]
        [StringLength(20)]
        public string? RecipientPhoneNumber { get; set; }

        /// <summary>
        /// 发送者ID
        /// </summary>
        public Guid? SenderId { get; set; }

        /// <summary>
        /// 发送者姓名
        /// </summary>
        [StringLength(100)]
        public string? SenderName { get; set; }

        /// <summary>
        /// 模板ID
        /// </summary>
        public Guid? TemplateId { get; set; }

        /// <summary>
        /// 模板数据
        /// </summary>
        public Dictionary<string, object>? TemplateData { get; set; }

        /// <summary>
        /// 通知参数
        /// </summary>
        public string? Parameters { get; set; }

        /// <summary>
        /// 附件列表
        /// </summary>
        public string? Attachments { get; set; }

        /// <summary>
        /// 调度时间
        /// </summary>
        public DateTime? ScheduledTime { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpirationTime { get; set; }

        /// <summary>
        /// 最大重试次数
        /// </summary>
        [Range(0, 10)]
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 分类标签
        /// </summary>
        [StringLength(100)]
        public string? Category { get; set; }

        /// <summary>
        /// 业务数据
        /// </summary>
        public string? BusinessData { get; set; }

        /// <summary>
        /// 是否静默发送
        /// </summary>
        public bool IsSilent { get; set; } = false;

        /// <summary>
        /// 是否需要确认
        /// </summary>
        public bool RequiresConfirmation { get; set; } = false;

        /// <summary>
        /// 确认截止时间
        /// </summary>
        public DateTime? ConfirmationDeadline { get; set; }

        /// <summary>
        /// 地理位置限制
        /// </summary>
        public string? LocationRestriction { get; set; }

        /// <summary>
        /// 时间限制
        /// </summary>
        public string? TimeRestriction { get; set; }

        /// <summary>
        /// 设备限制
        /// </summary>
        public string? DeviceRestriction { get; set; }

        /// <summary>
        /// 关联ID
        /// </summary>
        public Guid? RelatedEntityId { get; set; }

        /// <summary>
        /// 关联类型
        /// </summary>
        [StringLength(100)]
        public string? RelatedEntityType { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid? TenantId { get; set; }

        /// <summary>
        /// 自定义属性
        /// </summary>
        public Dictionary<string, object>? Properties { get; set; }

        /// <summary>
        /// 是否立即发送
        /// </summary>
        public bool SendImmediately { get; set; } = true;

        /// <summary>
        /// 自定义发送时间
        /// </summary>
        public DateTime? CustomSendTime { get; set; }

        /// <summary>
        /// 发送选项
        /// </summary>
        public NotificationSendOptions? SendOptions { get; set; }
    }

    /// <summary>
    /// 发送通知选项DTO
    /// </summary>
    public class NotificationSendOptions
    {
        /// <summary>
        /// 发送间隔（毫秒）
        /// </summary>
        public int SendIntervalMs { get; set; } = 1000;

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 重试间隔（毫秒）
        /// </summary>
        public int RetryIntervalMs { get; set; } = 5000;

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        public int TimeoutSeconds { get; set; } = 30;

        /// <summary>
        /// 是否跳过重复发送
        /// </summary>
        public bool SkipDuplicates { get; set; } = true;

        /// <summary>
        /// 是否跳过已退订用户
        /// </summary>
        public bool SkipUnsubscribed { get; set; } = true;

        /// <summary>
        /// 是否启用监控
        /// </summary>
        public bool EnableMonitoring { get; set; } = true;

        /// <summary>
        /// 自定义元数据
        /// </summary>
        public Dictionary<string, object>? Metadata { get; set; }
    }

    /// <summary>
    /// 通知发送结果DTO
    /// </summary>
    public class NotificationSendResultDto
    {
        /// <summary>
        /// 通知ID
        /// </summary>
        public Guid NotificationId { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 外部ID
        /// </summary>
        public string? ExternalId { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime SentTime { get; set; }

        /// <summary>
        /// 预估送达时间
        /// </summary>
        public DateTime? EstimatedDeliveryTime { get; set; }

        /// <summary>
        /// 失败原因
        /// </summary>
        public string? FailureReason { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public string? ErrorCode { get; set; }

        /// <summary>
        /// 错误详情
        /// </summary>
        public string? ErrorDetails { get; set; }

        /// <summary>
        /// 是否需要重试
        /// </summary>
        public bool WillRetry { get; set; }

        /// <summary>
        /// 下次重试时间
        /// </summary>
        public DateTime? NextRetryTime { get; set; }

        /// <summary>
        /// 当前重试次数
        /// </summary>
        public int RetryCount { get; set; }

        /// <summary>
        /// 发送耗时（毫秒）
        /// </summary>
        public long SendTimeMs { get; set; }

        /// <summary>
        /// 附加信息
        /// </summary>
        public Dictionary<string, object>? AdditionalData { get; set; }
    }
}